package com.xpgk.mvc.ddc.statistic.mdStatistic.mdDailyStastic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.statistic.ddStatistic.ddDailyStastic.DdDailyStasticSQL;
import com.xpgk.mvc.ddc.statistic.ddStatistic.ddMonthStatistic.DdMonthStasticSQL;
import com.xpgk.mvc.ddc.statistic.zdStatistic.zdDailyStastic.ZdDailyStasticXLS;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolDateTime;

public class MdDailyStasticController extends BaseController{

	public void index(){
		Organization org = Organization.dao.findById(getCUserOrganIds());
		setAttr("curOrgan", org);
		setAttr("startTime", ToolDateTime.getMonthOneDay(new Date()));
		render("mdDailyStastic.html");
	}
	
	public void getList(){
		String startTime = getPara("startTime");
		String organIds = getPara("organIds");
		String endTime = getPara("endTime");
		String sql = getSqlMy(MdDailyStasticSQL.sqlId_splitPageFrom);
		List<Record> result = Db.find(sql,startTime,endTime,organIds);
		renderJson(result);
	}
	
	
	public   void  excel() 
	{
		int zcgd = 0;
		int zygd = 0;
		int bggd = 0;
		int bhpgd = 0;
		int bzjgd = 0;
		int zxgd = 0;
		User user = User.dao.findById(getCUserIds());
		Organization org = Organization.dao.findById(getCUserOrganIds());
		String startTime = getPara("startTime");
		String organIds = getPara("organIds");
		String endTime = getPara("endTime");
		String sql = getSqlMy(MdDailyStasticSQL.sqlId_splitPageFrom);
		List<Record> result = Db.find(sql,startTime,endTime,organIds);
		if(result!=null && result.size()>0){
			for(Record record:result){
				if(record.getStr("zcgd")!=null && !record.getStr("zcgd").equals("")){
					zcgd = zcgd+Integer.parseInt(record.getStr("zcgd"));
				}
				if(record.getStr("zygd")!=null && !record.getStr("zygd").equals("")){
					zygd = zygd+Integer.parseInt(record.getStr("zygd"));
				}
				if(record.getStr("bggd")!=null && !record.getStr("bggd").equals("")){
					bggd = bggd+Integer.parseInt(record.getStr("bggd"));
				}
				if(record.getStr("bhpgd")!=null && !record.getStr("bhpgd").equals("")){
					bhpgd = bhpgd+Integer.parseInt(record.getStr("bhpgd"));
				}
				if(record.getStr("bzjgd")!=null && !record.getStr("bzjgd").equals("")){
					bzjgd = bzjgd+Integer.parseInt(record.getStr("bzjgd"));
				}
				if(record.getStr("zxgd")!=null && !record.getStr("zxgd").equals("")){
					zxgd = zxgd+Integer.parseInt(record.getStr("zxgd"));
				}
			}
		}
		Record r = new Record();
		r.set("bussdate","合计")
		.set("zcgd",zcgd)
		.set("zygd",zygd)
		.set("bggd",bggd)
		.set("bhpgd",bhpgd)
		.set("bzjgd",bzjgd)
		.set("zxgd",zxgd);
		result.add(r);
		//搜索条件字符串
		String  search="时间："+startTime+"~"+endTime+" 导出人："+user.getStr(User.column_name)+" 导出机构："+org.getStr(Organization.column_name);
		render(new MdDailyStasticXLS(result,search,org.getStr(Organization.column_name)+"日报表"+startTime+"~"+endTime));
	}
}
